package com.alibaba.algo.domain.user;

import com.alibaba.algo.constants.Constants;
import com.alibaba.algo.cooperation.feign.vo.req.SelectPositionListByUserReqVO;
import com.alibaba.algo.cooperation.feign.vo.req.SelectPositionUserReqVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.SelectPositionListRequestVO;
import com.alibaba.algo.core.base.BaseDomain;
import com.alibaba.algo.core.exception.BaseException;
import com.alibaba.algo.dao.user.SysPositionMapper;
import com.alibaba.algo.dtos.user.PositionUserDTO;
import com.alibaba.algo.dtos.user.SysPositionDTO;
import com.alibaba.algo.entity.user.SysPosition;
import com.alibaba.algo.exception.BaseErrorCode;
import com.alibaba.algo.util.DateConverterUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 类SysPosition.java的实现描述：岗位Domain
 *
 * @author nieganggang 2021-03-12 16:08:17
 */
@Component
public class SysPositionDomain extends BaseDomain<SysPosition> {

    @Resource
    private SysPositionMapper sysPositionMapper;
    @Autowired
    private SqlSessionFactory sqlSessionFactory;


    public Long selectOrgByPositionId(Long positionId) {
        if (ObjectUtils.isEmpty(positionId)) {
            throw new BaseException(BaseErrorCode.POSITION_ID_IS_NULL.getCode(), BaseErrorCode.POSITION_ID_IS_NULL.getMessage());
        }
        return sysPositionMapper.selectOrgByPositionId(positionId);
    }


    /**
     * 更新当前组织的主岗位为非主岗
     *
     * @param orgId 组织id
     * @return 更新结果
     */
    public Integer updateMainPositionByOrgId(Long orgId) {
        if (ObjectUtils.isEmpty(orgId)) {
            throw new BaseException(BaseErrorCode.ORG_ID_IS_NULL.getCode(), BaseErrorCode.ORG_ID_IS_NULL.getMessage());
        }
        return sysPositionMapper.updateMainPositionByOrgId(orgId);
    }

    public SysPosition getOne(SysPosition param) {
        if (null != param && ObjectUtils.isEmpty(param.getName())) {
            throw new BaseException(BaseErrorCode.NAME_IS_NULL.getCode(), BaseErrorCode.NAME_IS_NULL.getMessage());
        }
        return sysPositionMapper.getOne(param, null);
    }

    /**
     * 根据code或者id获取岗位详情
     *
     * @param param
     * @return
     */
    public SysPosition getPositionDetail(SysPosition param) {
        if (ObjectUtils.isEmpty(param)) {
            return new SysPosition();
        }
        return sysPositionMapper.getOne(param, null);
    }

    /**
     * 根据code或者id、isDeleted获取岗位详情
     *
     * @param param
     * @return
     */
    public SysPosition getPositionDetailWithIsDeleted(SysPosition param) {
        if (ObjectUtils.isEmpty(param)) {
            return new SysPosition();
        }
        return sysPositionMapper.getOneWithIsDeleted(param, null);
    }

    /**
     * 根据组织id查询岗位信息
     *
     * @param orgId
     * @return
     */
    public List<SysPosition> list(Long orgId) {
        SysPosition param = new SysPosition();
        param.setOrgId(orgId);
        return sysPositionMapper.listByPara(param, null);
    }

    /**
     * 根据组织id查询岗位信息
     *
     * @param orgId
     * @return
     */
    public List<SysPositionDTO> listPositionOrg(Long orgId) {
        SysPosition param = new SysPosition();
        param.setOrgId(orgId);
        return sysPositionMapper.listPositionOrgByPara(param, null);
    }

    /**
     * 根据岗位编码逻辑删除岗位信息
     *
     * @param posCode 岗位编码
     * @return 更新结果
     */
    public Integer deletePositionByPosCode(String posCode) {
        if (StringUtils.isEmpty(posCode)) {
            throw new BaseException(BaseErrorCode.POS_CODE_IS_NULL.getCode(), BaseErrorCode.POS_CODE_IS_NULL.getMessage());
        }
        return sysPositionMapper.updatePositionStatusByPosCode(posCode);
    }

    public Integer deleteByPosCode(String posCode) {
        if (StringUtils.isEmpty(posCode)) {
            throw new BaseException(BaseErrorCode.POS_CODE_IS_NULL.getCode(), BaseErrorCode.POS_CODE_IS_NULL.getMessage());
        }
        return sysPositionMapper.deleteByPosCode(posCode);
    }

    public Integer updateByPosCode(SysPosition sysPosition){
        return sysPositionMapper.updateByPosCode(sysPosition);
    }

    public Integer saveOrUpdatePositionForSync(SysPosition sysPosition) {
        LocalDateTime cutDateTime = DateConverterUtils.asLocalDateTime(new Date());
        sysPosition.setGmtCreated(cutDateTime);
        sysPosition.setGmtModified(cutDateTime);
        sysPosition.setCreator(Constants.ORIGIN_SAP);
        sysPosition.setModifier(Constants.ORIGIN_SAP);
        Integer updateResult = sysPositionMapper.updateByPosCode(sysPosition);
        if (null != updateResult && updateResult > 0) {
            return updateResult;
        } else {
            return sysPositionMapper.insert(sysPosition);
        }
    }

    /**
     * 批量保存
     *
     * @param positions
     * @return
     */
    public Long saveBatch(List<SysPosition> positions) {
        SqlSession sqlSession = null;
        try {
            sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
            SysPositionMapper mapper = sqlSession.getMapper(SysPositionMapper.class);
            for (SysPosition position : positions) {
                Integer insert = mapper.insert(position);
            }
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
        return Long.valueOf(positions.size());
    }

    public List<PositionUserDTO> getUsers(SelectPositionUserReqVO vo) {
        return sysPositionMapper.listUserByAppAndPosition(vo);
    }

    /**
     * 查询岗位用户列表
     *
     * @param vo
     * @return
     */
    public List<PositionUserDTO> selectPositionListByUser(SelectPositionListByUserReqVO vo) {
        return sysPositionMapper.selectPositionListByUser(vo);
    }

    /**
     * 获取岗位下的用户列表
     *
     * @param vo 岗位标识
     * @return 用户列表
     */
    public List<PositionUserDTO> selectUserListByPosition(SelectPositionUserReqVO vo) {
        return sysPositionMapper.selectUserListByPosition(vo);
    }

    /**
     * 获取岗位下的用户列表(支持全量查询)
     *
     * @param vo 岗位标识
     * @return 用户列表
     */
    public List<PositionUserDTO> selectUserListWithIsDeletedByPosition(SelectPositionUserReqVO vo) {
        return sysPositionMapper.selectUserListWithIsDeletedByPosition(vo);
    }

    public List<SysPositionDTO> selectListByOrgIdAndEmployeeId(Long orgId,Long employeeId) {
        return sysPositionMapper.selectListByOrgIdAndEmployeeId(orgId,employeeId);
    }

    public List<SysPosition> qryPositionListByOrgIdAndName(SelectPositionListRequestVO vo){
        return sysPositionMapper.qryPositionListByOrgIdAndName(vo);
    }
}
